//1.下载axios插件  npm i axios
//2.导入axios
import axios from "axios";

//3.实例化axios对象
var service = axios.create({
    timeout: 10 * 1000,
    baseURL: '/api',
})
//4.设置请求拦截器
service.interceptors.request.use((config) => {
    //统一设置请求头
    config.headers['token'] = '';
    return config;
}, (error) => {
    return Promise.reject(error);
})
//5.设置响应拦截器
service.interceptors.response.use((res) => {
    return res;
}, (error) => {
    if (error.response.status == 401) {
        console.log("身份认证过期，请重新登录");
        //xhr.status:获取当前服务器的响应状态  200=>成功
    }
    else if (error.response.status == 404) {
        console.log("访问路径有误！")
    } else if (error.response.status == 500) {
        console.log("服务器内部错误");
    }
})

//6.抛出axios实例化对象
export default service;